<script>
document.addEventListener("DOMContentLoaded", function () {
    layer.config({locale: '#(i18n.getTag())'})
    var headers = {
        'Content-Type': 'application/json',
        'language': '#(i18n.getTag())'
    };
    var jwt = localStorage.getItem("jwt");
    if (jwt) headers['Authorization'] = jwt;
    window.gateway = axios.create({
        baseURL: '#(GATEWAY)/',
        timeout: 10000,
        headers: headers,
        withCredentials: true
    });
    window.gateway.interceptors.request.use(function (request) {
        return request;
    }, function (error) {
        return Promise.reject(error);
    });
    window.gateway.interceptors.response.use(function (response) {
        var data = response.data;
        if (data && ['00000', '00001'].indexOf(data.code) === -1) layer.alert(data.message, {icon: 0})
        return response;
    }, function (error) {
        var response = error.response;
        if (response) { // 请求成功发出且服务器也响应了状态码，但状态代码超出了 2xx 的范围
            var data = response.data;
            var status = response.status;
            if (data && data.message) {
                layer.alert(data.message, {icon: 0});
            } else {
                switch (status) {
                    case 400:
                        layer.alert(response.statusText, {icon: 0});
                        break;
                    default:
                        break;
                }
            }
        } else {
            layer.alert(error.message, {icon: 0});  // 发送请求时出了点问题
        }
        return Promise.reject(error);
    });
})
</script>